package com.industrial.white.service.impl;


import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.industrial.common.core.utils.PageUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.system.api.domain.SysUser;
import com.industrial.system.api.model.LoginUser;
import com.industrial.white.domain.EmDataSubmission;
import com.industrial.system.api.domain.EmEnterpriseInformationCollection;
import com.industrial.white.domain.WhiteIndustrialArea;
import com.industrial.white.mapper.EmDataSubmissionMapper;
import com.industrial.white.mapper.EmEnterpriseInformationCollectionMapper;
import com.industrial.white.mapper.WhiteIndustrialAreaMapper;
import com.industrial.white.service.IEmDataSubmissionService;
import com.industrial.white.utils.DateUtils;
import com.industrial.white.vo.EmDataSubmissionMounthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * 数据报送Service业务层处理
 * 
 * @author Zhou_S
 * @date 2023-08-16
 */
@Service
public class EmDataSubmissionServiceImpl implements IEmDataSubmissionService
{
    @Autowired
    private EmDataSubmissionMapper emDataSubmissionMapper;
    @Autowired
    private EmEnterpriseInformationCollectionMapper emEnterpriseInformationCollectionMapper;

    @Autowired
    private WhiteIndustrialAreaMapper whiteIndustrialAreaMapper;

    @Value("${file.prefix}")
    private String filePrefix;

    @Override
    public List<EmDataSubmissionMounthVo> groupList(EmDataSubmission emDataSubmission) {

        List<EmDataSubmissionMounthVo> list=new ArrayList<>();
        //日期搜索
        if(StringUtils.isNotBlank(emDataSubmission.getStartTime())){
            emDataSubmission.setEndTime(DateUtils.getLastDateOfMonth(emDataSubmission.getStartTime()));
            emDataSubmission.setStartTime(DateUtils.getFirstDateOfMonth(emDataSubmission.getStartTime()));
        }
        //获取权限
        SysUser user= SecurityUtils.getLoginUser().getSysUser();
//        String  role= SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleKey();
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if(user.isAdmin()){
            roles.add("p_01_011");
        }

        if(ArrayUtil.contains(roles.toArray(),"p_01_011")){
            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getProvince());
            emDataSubmission.setProvince(whiteIndustrialArea.getBaseId());
            list=emDataSubmissionMapper.groupProvinceList(emDataSubmission);
        }else if(ArrayUtil.contains(roles.toArray(),"s_01_011")){
            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getCity());
            emDataSubmission.setCity(whiteIndustrialArea.getBaseId());
            list=emDataSubmissionMapper.groupCityList(emDataSubmission);
        }else if(ArrayUtil.contains(roles.toArray(),"x_01_011")){
            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getDistrict());
            emDataSubmission.setCounty(whiteIndustrialArea.getBaseId());
            list=emDataSubmissionMapper.groupCountyList(emDataSubmission);
        }else if(ArrayUtil.contains(roles.toArray(),"q_01_001")|| ArrayUtil.contains(roles.toArray(),"white")){

            EmEnterpriseInformationCollection emEnterpriseInformationCollection=
                    emEnterpriseInformationCollectionMapper.getEnterDataInfo(user.getUserName());

            if(emEnterpriseInformationCollection!=null){
                emDataSubmission.setEnterpeiseId(emEnterpriseInformationCollection.getBaseId());
            }
            list=emDataSubmissionMapper.groupEnterpriseList(emDataSubmission);
        }

//        if("p_01_011".contains(role)){
//            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getProvince());
//            emDataSubmission.setProvince(whiteIndustrialArea.getBaseId());
//            list=emDataSubmissionMapper.groupProvinceList(emDataSubmission);
//        }else if("s_01_011".contains(role)){
//            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getCity());
//            emDataSubmission.setCity(whiteIndustrialArea.getBaseId());
//            list=emDataSubmissionMapper.groupCityList(emDataSubmission);
//        }else if("x_01_011".contains(role)){
//            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getDistrict());
//            emDataSubmission.setCounty(whiteIndustrialArea.getBaseId());
//            list=emDataSubmissionMapper.groupCountyList(emDataSubmission);
//        }else if("q_01_001".contains(role)||"white".contains(role)){
//
//            EmEnterpriseInformationCollection emEnterpriseInformationCollection=
//                    emEnterpriseInformationCollectionMapper.getEnterDataInfo(user.getUserName());
//
//            if(emEnterpriseInformationCollection!=null){
//                emDataSubmission.setEnterpeiseId(emEnterpriseInformationCollection.getBaseId());
//            }
//            list=emDataSubmissionMapper.groupEnterpriseList(emDataSubmission);
//        }
        return list;
    }

    /**
     * 查询数据报送
     * 
     * @param baseId 数据报送主键
     * @return 数据报送
     */
    @Override
    public EmDataSubmission selectEmDataSubmissionByBaseId(String baseId)
    {
        return emDataSubmissionMapper.selectEmDataSubmissionByBaseId(baseId);
    }


    /**
     * 查询数据报送列表
     * 
     * @param emDataSubmission 数据报送
     * @return 数据报送
     */
    @Override
    public List<EmDataSubmission> selectEmDataSubmissionList(EmDataSubmission emDataSubmission)
    {
        List<EmDataSubmission> list=new ArrayList<>();
        //日期搜索
        if(StringUtils.isNotBlank(emDataSubmission.getStartTime())){
            emDataSubmission.setEndTime(DateUtils.getLastDateOfMonth(emDataSubmission.getStartTime()));
            emDataSubmission.setStartTime(DateUtils.getFirstDateOfMonth(emDataSubmission.getStartTime()));
        }
        SysUser user= SecurityUtils.getLoginUser().getSysUser();
//        String  role= SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleKey();
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if(user.isAdmin()){
            roles.add("p_01_011");
        }

        if(ArrayUtil.contains(roles.toArray(),"p_01_011")){
            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getProvince());
               emDataSubmission.setProvince(whiteIndustrialArea.getBaseId());
        }else if(ArrayUtil.contains(roles.toArray(),"s_01_011")){
            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getCity());
                emDataSubmission.setCity(whiteIndustrialArea.getBaseId());
        }else if(ArrayUtil.contains(roles.toArray(),"x_01_011")){
            WhiteIndustrialArea whiteIndustrialArea= whiteIndustrialAreaMapper.getAreaByCode(user.getDistrict());
               emDataSubmission.setCounty(whiteIndustrialArea.getBaseId());
        }else if(ArrayUtil.contains(roles.toArray(),"q_01_001")){
            EmEnterpriseInformationCollection emEnterpriseInformationCollection=  emEnterpriseInformationCollectionMapper.getEnterDataInfo(user.getUserName());
            if(emEnterpriseInformationCollection == null){
                return new ArrayList<>();
            }
            if(emEnterpriseInformationCollection!=null){
                emDataSubmission.setEnterpeiseId(emEnterpriseInformationCollection.getBaseId());
            }
        }else if(ArrayUtil.contains(roles.toArray(),"white")){
            EmEnterpriseInformationCollection emEnterpriseInformationCollection=  emEnterpriseInformationCollectionMapper.getEnterDataInfo(user.getUserName());
            if(emEnterpriseInformationCollection == null){
                return new ArrayList<>();
            }
            if(emEnterpriseInformationCollection!=null){
                emDataSubmission.setEnterpeiseId(emEnterpriseInformationCollection.getBaseId());
            }
        }
        PageUtils.startPage();
        list=  emDataSubmissionMapper.selectEmDataSubmissionList(emDataSubmission);
        return list;
    }

    /**
     * 新增数据报送
     * 
     * @param emDataSubmission 数据报送
     * @return 结果
     */
    @Override
    public int insertEmDataSubmission(EmDataSubmission emDataSubmission)
    {

        SysUser user= SecurityUtils.getLoginUser().getSysUser();

        EmEnterpriseInformationCollection emEnterpriseInformationCollection=
                emEnterpriseInformationCollectionMapper.getEnterDataInfo(user.getUserName());
        EmDataSubmission submission =new EmDataSubmission();

        submission.setStartTime(DateUtils.getFirstDateOfMonth(DateUtils.getMonth()));
        submission.setEndTime(DateUtils.getLastDateOfMonth(DateUtils.getMonth()));
        submission.setEnterpeiseId(emEnterpriseInformationCollection.getBaseId());
        List<EmDataSubmission> list= emDataSubmissionMapper.selectEmDataSubmissionList(submission);
        int flag=0;
        if(list.size()>0){
        }else{
            String id= UUID.randomUUID().toString().replaceAll("-","");
            emDataSubmission.setBaseId(id);
            emDataSubmission.setEnterpeiseId(emEnterpriseInformationCollection.getBaseId());
            emDataSubmission.setBaseCreateTime(DateUtils.getNowDate());
            emDataSubmission.setBaseUpdateTime(DateUtils.getNowDate());
            if(StringUtils.isNotBlank(emDataSubmission.getFjPath())){
                String fjPath = emDataSubmission.getFjPath();
                emDataSubmission.setFjPath(StrUtil.removePrefix(fjPath,filePrefix));
            }
            flag= emDataSubmissionMapper.insertEmDataSubmission(emDataSubmission);
        }
        return flag;
    }

    /**
     * 修改数据报送
     * 
     * @param emDataSubmission 数据报送
     * @return 结果
     */
    @Override
    public int updateEmDataSubmission(EmDataSubmission emDataSubmission)
    {
        if(StringUtils.isNotBlank(emDataSubmission.getFjPath())){
            if(StrUtil.startWith(emDataSubmission.getFjPath(),"http")){
                String fjPath = emDataSubmission.getFjPath();
                emDataSubmission.setFjPath(StrUtil.removePrefix(fjPath,filePrefix));
            }else{
                String fjPath = emDataSubmission.getFjPath();
                emDataSubmission.setFjPath(StrUtil.removePrefix(fjPath,"/dev-api/file"));
            }

        }
        return emDataSubmissionMapper.updateEmDataSubmission(emDataSubmission);
    }

    /**
     * 批量删除数据报送
     * 
     * @param baseIds 需要删除的数据报送主键
     * @return 结果
     */
    @Override
    public int deleteEmDataSubmissionByBaseIds(String[] baseIds)
    {
        return emDataSubmissionMapper.deleteEmDataSubmissionByBaseIds(baseIds);
    }

    /**
     * 删除数据报送信息
     * 
     * @param baseId 数据报送主键
     * @return 结果
     */
    @Override
    public int deleteEmDataSubmissionByBaseId(String baseId)
    {
        return emDataSubmissionMapper.deleteEmDataSubmissionByBaseId(baseId);
    }

    @Override
    public boolean updateByBaseIds(EmDataSubmission[] emDataSubmissions) {
        int updates=0;
        boolean flag=false;
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        for (int i = 0; i <emDataSubmissions.length ; i++) {
            updates+=1;
            if(roles.contains("p_01_011")){
                emDataSubmissions[i].setProvinceStatus("1");
            }else if(roles.contains("s_01_011")){
                emDataSubmissions[i].setProvinceStatus("2");
            }else if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
                emDataSubmissions[i].setProvinceStatus("3");
            }
            emDataSubmissionMapper.updateEmDataSubmission(emDataSubmissions[i]);
        }
        if(updates==emDataSubmissions.length){
          flag=true;
        }
        return  flag;
    }
}
